package com.jz.community.moduleshopping.confirmOrder.ui;

import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.bean.LoginInfo;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.task.GetSmsCodeTask;
import com.jz.community.basecomm.task.GetValidateSmsCodeTask;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.basecomm.utils.TimeUtils;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/8/20 15:19
 */
public class CardOrderPhoneActivity extends BaseMvpActivity {

    @BindView(R2.id.title_back_left)
    ImageView titleBackLeft;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.title_right)
    TextView titleRight;
    @BindView(R2.id.title_right_iv)
    ImageView titleRightIv;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R2.id.et_change_phone)
    EditText etChangePhone;
    @BindView(R2.id.tv_change_phone_code)
    TextView tvChangePhoneCode;
    @BindView(R2.id.et_change_phone_code)
    EditText etChangePhoneCode;
    @BindView(R2.id.v_change_phone)
    View vChangePhone;
    @BindView(R2.id.tv_change_phone_getcode)
    TextView tvChangePhoneGetcode;
    private TimeUtils timeUtils;

    @Override
    protected int getContentView() {
        return R.layout.module_shopping_activity_cardbag_change_phone;
    }

    @Override
    protected void initView() {
        super.initView();
        titleName.setText("更好手机号码");
        titleRight.setText("保存");
        SHelper.vis(titleRight);
        setImmersionBar(titleToolbar);
    }

    @Override
    protected void initDatas() {
        super.initDatas();
    }

    @Override
    protected void addListener() {
        super.addListener();
        titleBackLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tvChangePhoneGetcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isJudgePhone()) {
                    getSmsCode();
                }
            }
        });

        titleRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isJudgeCode()) {
                    validateSmsCode();
                }
            }
        });
    }

    private boolean isJudgePhone() {
        if (Preconditions.isNullOrEmpty(etChangePhone.getText().toString().trim())) {
            WpToast.l(CardOrderPhoneActivity.this, "手机号不能为空");
            return false;
        }
        return true;
    }

    private boolean isJudgeCode() {
        if (Preconditions.isNullOrEmpty(etChangePhoneCode.getText().toString().trim())) {
            WpToast.l(CardOrderPhoneActivity.this, "验证码不能为空");
            return false;
        }
        return true;
    }

    private void validateSmsCode() {
        new GetValidateSmsCodeTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                BaseResponseInfo baseResponseInfo = (BaseResponseInfo) o;
                if (!Preconditions.isNullOrEmpty(baseResponseInfo)) {
                    if (baseResponseInfo.getResult().equals("0000")) {
                        BaseSpUtils.getInstance().setCardPhone(CardOrderPhoneActivity.this, etChangePhone.getText().toString().trim());
                        WpToast.l(CardOrderPhoneActivity.this, "保存成功");
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        WpToast.l(CardOrderPhoneActivity.this, baseResponseInfo.getMessage());
                    }
                } else {
                    WpToast.l(CardOrderPhoneActivity.this, "保存失败");
                }
            }
        }).execute(etChangePhoneCode.getText().toString().trim(), etChangePhone.getText().toString().trim());
    }


    private void getSmsCode() {
        new GetSmsCodeTask(this, false, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                LoginInfo loginInfo = (LoginInfo) o;
                if (!Preconditions.isNullOrEmpty(loginInfo)) {
                    handelSmsCodeState(loginInfo);
                }
            }
        }).execute("2", etChangePhone.getText().toString().trim());
    }

    /**
     * 验证码结果处理状态
     *
     * @param loginInfo
     */
    private void handelSmsCodeState(LoginInfo loginInfo) {
        if (!Preconditions.isNullOrEmpty(loginInfo.getVcode())) {
            if (timeUtils == null) {
                WpToast.l(this, "验证码发送成功");
                timeUtils = new TimeUtils(120 * 1000, 1000, CardOrderPhoneActivity.this, tvChangePhoneGetcode);
            }
            timeUtils.start();
        } else {
            WpToast.l(this, loginInfo.getMessage());
        }
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeUtils != null) {
            timeUtils.cancel();
        }
    }
}
